//
//  TCSpecialHotTopicViewController.m
//  Project_B
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "TCSpecialHotTopicViewController.h"
#import "AFNetworkHandler.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "TCAllTableViewCell.h"
#import "TCTopicViewController.h"
#import "FirstSpecialViewController.h"
#import "CalculateHeight.h"

#import "FirstTopViewViewController.h"
//#define WIDTH [[UIScreen mainScreen] bounds].size.width / 375
//#define HEIGHT [[UIScreen mainScreen] bounds].size.height / 667
@interface TCSpecialHotTopicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)MBProgressHUD *HUD;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSDictionary *dic;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSMutableArray *imArr;
@end

@implementation TCSpecialHotTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.imArr = [NSMutableArray array];
    self.dataArr = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    
    [self getData];
}
- (void)creat
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, (667-49)*HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[TCAllTableViewCell class] forCellReuseIdentifier:@"cell"];
//    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, 300*HEIGHT)];
    UIView *view = [[UIView alloc]init];
//    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, 200*HEIGHT)];
//    [image sd_setImageWithURL:[NSURL URLWithString:self.dic[@"data"][@"specialinfo"][@"image"]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
//    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(40*WIDTH, 70*HEIGHT, 355*WIDTH, 40*HEIGHT)];
//    title.textAlignment = NSTextAlignmentCenter;
//    title.text = self.dic[@"data"][@"specialinfo"][@"title"];
//    title.textColor = [UIColor whiteColor];
//    [title setFont:[UIFont boldSystemFontOfSize:18]];
//    [image addSubview:title];
//    UILabel *sublabel = [[UILabel alloc]initWithFrame:CGRectMake(70*WIDTH, 100*HEIGHT, 235*WIDTH, 40*HEIGHT)];
//    sublabel.text = self.dic[@"data"][@"specialinfo"][@"subhead"];
//    sublabel.textColor = [UIColor whiteColor];
//    [image addSubview:sublabel];
//    [view addSubview:image];
    
    CGFloat h = [CalculateHeight heightWithText:self.dic[@"data"][@"specialinfo"][@"describe"] fontSize:14];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTH, 50*HEIGHT, 355*WIDTH, h)];
    label.textColor = [UIColor grayColor];
    label.numberOfLines = 0;
    label.text = self.dic[@"data"][@"specialinfo"][@"describe"];
//    [label sizeToFit];
    [label setFont:[UIFont systemFontOfSize:14]];
    [view addSubview:label];
    
    UILabel *daoyu = [[UILabel alloc]initWithFrame:CGRectMake((375/2-30)*WIDTH, 10*HEIGHT, 60, 30)];
    daoyu.text = @"导语";
    daoyu.textColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
    [daoyu setFont:[UIFont systemFontOfSize:18]];
    [view addSubview:daoyu];
    view.frame = CGRectMake(0, 0, 375*WIDTH, h + 50);
    self.tableView.tableHeaderView = view;
}
- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/topicSpecialList/%@/0/", self.temp.detailid];
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
        self.dic = result;
        [self.HUD hide:YES];
        [self creat];
        [self.dataArr addObjectsFromArray:result[@"data"][@"topiclist"]];
        self.imArr = [TCTopic transformWithArray:result[@"data"][@"topiclist"]];
        [self.tableView reloadData];
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    TCTopicViewController *secVC = [[TCTopicViewController alloc]init];
//    secVC.temp = self.imArr[indexPath.row];
    FirstTopViewViewController *secVC = [[FirstTopViewViewController alloc] init];
    secVC.idString = [self.imArr[indexPath.row] topic_id];
    secVC.typeStr = ((TCTopic *)self.imArr[indexPath.row]).type;
    [self.navigationController pushViewController:secVC animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"~~~%ld", self.dataArr.count);
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210*HEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TCAllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.dic = self.dataArr[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
